package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.excepiton.ConstantEnum;
import com.chenyaowei.happyhub.mapper.AccountMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodVersionMapper;
import com.chenyaowei.happyhub.mapper.OrderMapper;
import com.chenyaowei.happyhub.pojo.GoodsPojo.GoodVersion;
import com.chenyaowei.happyhub.pojo.wechatuser.Account;
import com.chenyaowei.happyhub.pojo.wechatuser.Order;
import com.chenyaowei.happyhub.pojo.wechatuser.PayList;
import com.chenyaowei.happyhub.service.GoodsService.GoodService;
import com.chenyaowei.happyhub.utils.JwtTokenUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wechat")
public class WeChatPayController {
    @Resource
    GoodVersionMapper goodVersionMapper;
    @Resource
    OrderMapper orderMapper;
    @Autowired
    JwtTokenUtils jwtTokenUtils;
    @Resource
    AccountMapper accountMapper;
    @Resource
    GoodService goodService;
    ObjectMapper mapper = new ObjectMapper();
    Map<String, String> message = new HashMap<>();

    @RequestMapping("/checkNum")
    String checkNum(String payList) throws JsonProcessingException {
        List<Map> list = mapper.readValue(payList, List.class);
        System.out.println(list);
        for (Map map : list) {
            GoodVersion goodVersion = goodVersionMapper.selectGoodVersionByVersionId(Integer.parseInt(map.get("versionId").toString()));
            if (goodVersion.getCount() < Integer.parseInt(map.get("select_num").toString())) {
                message.put("status", "fail");
                message.put("goodName", (String) map.get("goods_name"));
                message.put("version", (String) map.get("memory"));
                message.put("count", String.valueOf(goodVersion.getCount()));
                return mapper.writeValueAsString(message);
            }
        }
        message.put("status", "ok");
        return mapper.writeValueAsString(message);
    }

    @RequestMapping("/pay")
    String pay(String payList, String receiverName, String mobile, String addressDetail, String city, String province, HttpServletRequest request) throws JsonProcessingException {
        List<Map> list = mapper.readValue(payList, List.class);
        String token = request.getHeader(ConstantEnum.AUTHORIZATION.getValue());
        String openid = jwtTokenUtils.getUsernameFromToken(token);
        Account account = accountMapper.selectAccountByOpenid(openid);
        if (!account.isPay()) {
            message.put("locked", "true");
            message.put("lockedMessage", "账号已被锁定,禁止交易");
            return mapper.writeValueAsString(message);
        }
        for (Map map : list) {
            float totalPrice=Float.parseFloat(map.get("price").toString()) * Float.parseFloat(map.get("select_num").toString());
            Order order = new Order(account.getId()
                    , Integer.parseInt(map.get("goodId").toString())
                    , map.get("goods_name").toString()
                    , map.get("memory").toString()
                    , Float.parseFloat(map.get("price").toString())
                    , Integer.parseInt(map.get("select_num").toString())
                    , totalPrice
                    , receiverName, mobile, city, province, addressDetail,showTime());
            orderMapper.insertOrder(order);
            System.out.println(Integer.parseInt(map.get("select_num").toString()));
            System.out.println(totalPrice);
            System.out.println(Integer.parseInt(map.get("goodId").toString()));
            goodService.updateSalesVolume(Integer.parseInt(map.get("select_num").toString()),totalPrice,map.get("goodId").toString());
            goodVersionMapper.updateSalesVolume(map.get("select_num").toString(),Integer.parseInt(map.get("select_num").toString()),totalPrice,Integer.parseInt(map.get("versionId").toString()));
        }
        return null;
    }
    public String showTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        date.getTime();
        String time=df.format(date);
        return time;
    }

    @RequestMapping("/orderlist")
    String orderList(HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader(ConstantEnum.AUTHORIZATION.getValue());
        String openid = jwtTokenUtils.getUsernameFromToken(token);
        Account account = accountMapper.selectAccountByOpenid(openid);
        List<Order> orderList = orderMapper.selectOrderById(account.getId());
        return mapper.writeValueAsString(orderList);

    }
}
